/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package babydisco.Math;

/**
 *  Class used to save complex numbers in given that java can't do that already
 * @author Florian
 */
public class Complex
{

    public double imaginair; //complex part
    public double real; //real part

    public Complex(double r, double i)
    {
        imaginair = i;
        real = r;
    }

    /**
     * add two complex numbers a & b
     * @param a
     * @param b
     * @return the result as a complex number
     */
    public static Complex add(Complex a, Complex b)
    {
        return new Complex(a.real + b.real, a.imaginair + b.imaginair);
    }

    /**
     * substract two complex numbers a & b
     * @param a
     * @param b
     * @return the result as a complex number
     */
    public static Complex subtract(Complex a, Complex b)
    {
        return new Complex(a.real - b.real, a.imaginair - b.imaginair);
    }

    /**
     * multiply two complex numbers a & b
     * @param a
     * @param b
     * @return the result as a complex number
     */
    public static Complex multiply(Complex a, Complex b)
    {
        return new Complex(a.real * b.real - a.imaginair * b.imaginair, a.real * b.imaginair + b.real * a.imaginair);
    }

    /**
     * Divide two complex numbers a & b
     * @param a
     * @param b
     * @return the result as a complex number
     */
    public static Complex divide(Complex a, Complex b)
    {
        double divider = b.imaginair * b.imaginair + b.real * b.real;
        return new Complex((a.real * b.real + a.imaginair * b.imaginair) / divider, (a.imaginair * b.real - a.real * b.imaginair) / divider);
    }

    /**
     * get the absolute value of a complex number
     * @param a
     * @return the absolute value as a double
     */
    public static double absolute(Complex a)
    {
        return Math.sqrt(a.real * a.real + a.imaginair * a.imaginair);
    }
}
